<?php

namespace app\admin\controller;

use app\admin\service\GoodsCateService;
use support\spark\model\goods\GoodsCate;
use support\Request;
use support\View;

class GoodsCateController extends Curd
{

    public function index()
    {

        if (request()->isAjax()) {
            $goodsCate = new GoodsCate();
            $list = $goodsCate->getAllList([], ["*"], ["id" => "asc"]);
            return sparkSuccess(dataReturn(0, 'success', makeTree($list)));
        }
        return view('goods_cate/index');
    }
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $goodsCateService = new GoodsCateService();
            $res = $goodsCateService->addGoodsCate($param);
            return sparkSuccess($res);
        }
        View::assign([
            'pid' => request()->input('pid', 0),
            'pName' => request()->input('pname', ''),
        ]);
        return view("goods_cate/add");
    }

    public function edit()
    {
        if (request()->isAjaxPost()) {
            $param =  request()->post();
            $goodsCateService = new GoodsCateService();
            $res = $goodsCateService->editGoodsCate($param);
            return sparkSuccess($res);
        }

        $pid =  request()->input('pid');
        $id =  request()->input('id');
        $goodsCateModel = new GoodsCate();
        if (0 == $pid) {
            $pName = '顶级分类';
        } else {
            $pName = $goodsCateModel->getInfoById($pid)['name'];
        }
        View::assign([
            'info' => json_encode($goodsCateModel->getInfoByWhere([
                'id' => $id
            ])),
            'pid' => $pid,
            'pName' => $pName,
        ]);
        return view("goods_cate/edit");
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = request()->input('id');

        $goodsCateService = new GoodsCateService();
        $res = $goodsCateService->delGoodsCate($id);
        return sparkSuccess($res);
    }
}